package com.hsy.crm.web.service.asyn;

import com.google.common.collect.Lists;
import com.hsy.crm.web.domain.cm.App;
import com.hsy.crm.web.domain.entity.*;
import com.hsy.crm.web.domain.ex.ServiceException;
import com.hsy.crm.web.mapper.*;
import com.hsy.crm.web.service.asyn.inter.IOrderService;
import com.hsy.crm.web.service.impl.StoreProductService;
import com.hsy.crm.web.utils.CalcUtils;
import com.hsy.crm.web.utils.UuidUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class AsynOrderService implements IOrderService {

    @Autowired
    StoreProductService storeProductService;

    @Autowired
    OrderDetailMapper orderDetailMapper;

    @Autowired
    StoreProductDetailMapper storeProductDetailMapper;

    @Autowired
    ProductMapper productMapper;

    @Autowired
    VendorMapper vendorMapper;

    @Autowired
    CustomerMapper customerMapper;

    @Autowired
    OrderMapper orderMapper;

    /**
     * 审核 添加明细  反审核只查询明细  明细的修改 删除 由各自的实现类完成
     * @param message
     * @return
     */
    @Override
    public List<StoreProductDetail> addStock(Message message) {

        if(message.getActionType() == App.ExamType.INVALID){
            return getStoreProductDetail(message);
        }

        List<StoreProductDetail> storeProductDetails = Lists.newArrayList();
        int direction = this.getDirection(message);

        List<OrderDetail> orderDetails = (List<OrderDetail>)message.getOrderDetails();
        for( OrderDetail orderDetail : orderDetails ){
            this.updateLastPrice(message,orderDetail);
            StoreProductDetail storeProductDetail = this.getStoreProductDetail(message,orderDetail,direction);
            storeProductDetail.setOrderId(orderDetail.getOrderId());
            storeProductDetails.add(storeProductDetail);
            storeProductDetailMapper.insert(storeProductDetail);
        }

        return storeProductDetails;
    }

    @Override
    public List<StoreProductDetail> getStoreProductDetail(Message message) {
        List<StoreProductDetail> storeProductDetails = Lists.newArrayList();

        List<OrderDetail> orderDetails = this.getOrderDetails(message);
        for (OrderDetail orderDetail : orderDetails){
            StoreProduct storeProduct = this.getStoreProduct(message,orderDetail);
            Long storeProductId = storeProductService.selectStoreProduct(storeProduct);

            StoreProductDetail storeProductDetail = new StoreProductDetail();
            storeProductDetail.setCustomerId(message.getCustomerId());
            storeProductDetail.setStoreProductId(storeProductId);
            storeProductDetail.setOrderDetailId(orderDetail.getId());
            storeProductDetail = storeProductDetailMapper.selectOne(storeProductDetail);
            storeProductDetails.add(storeProductDetail);
        }
        return storeProductDetails;
    }

    @Override
    public void relationAccount(Message message) {
        Order dbOrder = new Order();
        dbOrder.setCustomerId(message.getCustomerId());
        dbOrder.setId(message.getTargetId());
        dbOrder = orderMapper.selectByPrimaryKey(dbOrder);
        dbOrder.setCustomerId(message.getCustomerId());
        //订单金额
        long dueMoney = dbOrder.getDueMoney();
        //优惠金额
        long discountMoney = dbOrder.getDiscountMoney();
        //已收金额
        long realMoney = dbOrder.getRealMoney();
        if( discountMoney + realMoney < dueMoney ){
            long debtMoney = dueMoney - (discountMoney + realMoney);
            //付少了  欠款
            if( dbOrder.getOrderType() == App.BILL_TYPE.ORDER_JHD ){
                Vendor vendor = new Vendor();
                vendor.setId(dbOrder.getRelationId());
                vendor.setDebtMoney(-debtMoney);
                vendor.setCustomerId(dbOrder.getCustomerId());
                vendorMapper.updateDebtMoney(vendor);
            }else if( dbOrder.getOrderType() == App.BILL_TYPE.ORDER_JHTHD ){
                Vendor vendor = new Vendor();
                vendor.setId(dbOrder.getRelationId());
                vendor.setDebtMoney(debtMoney);
                vendor.setCustomerId(dbOrder.getCustomerId());
                vendorMapper.updateDebtMoney(vendor);
            }else if( dbOrder.getOrderType() == App.BILL_TYPE.ORDER_SHD ){
                Customer customer = new Customer();
                customer.setId(dbOrder.getRelationId());
                customer.setDebtMoney(debtMoney);
                customer.setCustomerId(dbOrder.getCustomerId());
                customerMapper.updateDebtMoney(customer);
            }else if( dbOrder.getOrderType() == App.BILL_TYPE.ORDER_SHTHD ){
                Customer customer = new Customer();
                customer.setId(dbOrder.getRelationId());
                customer.setDebtMoney(-debtMoney);
                customer.setCustomerId(dbOrder.getCustomerId());
                customerMapper.updateDebtMoney(customer);
            }
        }
    }

    private void updateLastPrice(Message message,OrderDetail orderDetail){
        Product product = new Product();
        product.setId(orderDetail.getProductId());
        product.setCustomerId(message.getCustomerId());
        if( message.getTargetType() == App.BILL_TYPE.ORDER_JHD ){
            product.setLastPurchasePrice(orderDetail.getPrice());
            productMapper.updateLastPurchasePrice(product);
        }else if( message.getTargetType() == App.BILL_TYPE.ORDER_SHD ){
            product.setLastSalePrice(orderDetail.getPrice());
            productMapper.updateLastSalePrice(product);
        }

    }

    public static int getDirection(int actionType,int targetType){
        Message message = new Message();
        message.setActionType(actionType);
        message.setTargetType(targetType);
        return getDirection(message);
    }

    public static int getDirection(Message message){
        int direction;

        if( message.getActionType() != App.ExamType.EXAMINE && message.getActionType() != App.ExamType.INVALID ){
            throw new ServiceException("message action type error");
        }

        if( message.getTargetType() == App.BILL_TYPE.ORDER_JHD ){
            if( message.getActionType() == App.ExamType.EXAMINE ){
                direction = App.StoreType.IN;
            }else{
                direction = App.StoreType.OUT;
            }
        }else if( message.getTargetType() == App.BILL_TYPE.ORDER_SHD ){
            if( message.getActionType() == App.ExamType.EXAMINE ){
                direction = App.StoreType.OUT;
            }else{
                direction = App.StoreType.IN;
            }
        }else if( message.getTargetType() == App.BILL_TYPE.ORDER_JHTHD ){
            if( message.getActionType() == App.ExamType.EXAMINE ){
                direction = App.StoreType.OUT;
            }else{
                direction = App.StoreType.IN;
            }
        }else if( message.getTargetType() == App.BILL_TYPE.ORDER_SHTHD ){
            if( message.getActionType() == App.ExamType.EXAMINE ){
                direction = App.StoreType.IN;
            }else{
                direction = App.StoreType.OUT;
            }
        }else{
            throw new ServiceException("message target type error");
        }
        return direction;
    }

    private List<OrderDetail> getOrderDetails(Message message){
        OrderDetail orderDetail = new OrderDetail();
        orderDetail.setCustomerId(message.getCustomerId());
        orderDetail.setOrderId(message.getTargetId());
        return orderDetailMapper.select(orderDetail);
    }

    private static StoreProduct getStoreProduct(Message message,OrderDetail orderDetail){
        StoreProduct storeProduct = new StoreProduct();
        storeProduct.setCompanyId(orderDetail.getCompanyId());
        storeProduct.setProductId(orderDetail.getProductId());
        storeProduct.setStoreId(orderDetail.getStoreId());
        storeProduct.setUnitId(orderDetail.getUnitId());
        storeProduct.setCustomerId(message.getCustomerId());
        return storeProduct;
    }

    private StoreProductDetail getStoreProductDetail(Message message,OrderDetail orderDetail, int type){
        StoreProductDetail storeProductDetail = new StoreProductDetail();
        storeProductDetail.setId(UuidUtils.getUuid());
        storeProductDetail.setBackNumber(0L);
        storeProductDetail.setBatchNo(orderDetail.getBatchNo());
        storeProductDetail.setProductDate(orderDetail.getProductionDate());
        storeProductDetail.setNumber(orderDetail.getNumber());
        storeProductDetail.setPrice(this.getPrice(orderDetail));
        storeProductDetail.setSort(orderDetail.getSort());
        storeProductDetail.setRelationStartSort(-1L);
        storeProductDetail.setRelationEndSort(-1L);
        storeProductDetail.setCustomerId(message.getCustomerId());
        storeProductDetail.setStoreProductId(orderDetail.getStoreProductId());
        storeProductDetail.setType(type);
        storeProductDetail.setTime(message.getTime());
        storeProductDetail.setOrderType(orderDetail.getOrderType());
        storeProductDetail.setOrderDetailId(orderDetail.getId());
        storeProductDetail.setOutPrice(0L);
        storeProductDetail.setOutNumber(0L);
        storeProductDetail.setTmpPrice(0L);
        storeProductDetail.setRushBack(App.Booleans.NO);
        return storeProductDetail;
    }

    public static Long getPrice(OrderDetail orderDetail){
        double totalPrice = CalcUtils.mul(orderDetail.getNumber(),orderDetail.getPrice());
        totalPrice = CalcUtils.div(totalPrice,App.DECIMAL.DIV);
        totalPrice = CalcUtils.sub(totalPrice,orderDetail.getPercentPrice());
        //totalPrice = CalcUtils.sub(totalPrice,orderDetail.getPurPercentPrice());   用户选择
        totalPrice = CalcUtils.sub(totalPrice,orderDetail.getDiscountMoney());
        return ((Double)totalPrice).longValue();
    }
}
